package br.com.bco.web.page.cobrador;

import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;

import br.com.bco.controle.funcionario.CobradorControl;
import br.com.bco.negocio.Cobrador;
import br.com.bco.negocio.exception.NegocioException;
import br.com.bco.web.page.AbstractPage;

public class InserirCobrador extends AbstractPage {

    private final Cobrador cobrador = new Cobrador();

    private TextField nome;
    private TextField cpf;
    private TextField matricula;
    private TextField dtu;
    private TextField horaEntrada;

    @SpringBean
    private CobradorControl cobradorControl;

    public InserirCobrador() {
        Form form = new Form("form", new CompoundPropertyModel(cobrador));
        adicionarCampos(form);
        adicionarBotoes(form);
        add(form);
    }

    private void adicionarCampos(Form form) {
        nome = new TextField("nome");
        cpf = new TextField("cpf");
        matricula = new TextField("matricula");
        dtu = new TextField("dtu");
        horaEntrada = new TextField("horaEntrada");
        form.add(nome);
        form.add(cpf);
        form.add(matricula);
        form.add(dtu);
        form.add(horaEntrada);
    }

    private void adicionarBotoes(Form form) {
        Button btnGravar = new Button("btnGravar", new Model()) {
            @Override
            public void onSubmit() {
                try {
                    cobradorControl.inserir(cobrador);
                } catch (NegocioException e) {
                    System.out.println(e.getMensagens().get(0));
                }
                setResponsePage(InserirCobrador.class);
            }
        };
        form.add(btnGravar);
    }

    @Override
    public String getTitulo() {
        return "Inserir Cobrador";
    }
}
